import { db } from '../index';
import { products, productCategories } from '../schema';
import { createId } from '@paralleldrive/cuid2';
import { eq } from 'drizzle-orm';

/**
 * 产品种子数据
 */
export async function runProductSeeds() {
  try {
    console.log('🔄 创建产品种子数据...');
    
    // 获取分类ID
    const categories = await db.select().from(productCategories);
    const getCategoryId = (name: string) => {
      const category = categories.find(c => c.name === name);
      return category?.id || null;
    };
    
    const productData = [
      {
        id: createId(),
        title: '发光字',
        type: '光电标识',
        categoryId: getCategoryId('光电标识'),
        description: '高品质LED发光字制作，多种工艺可选，适用于门头招牌、楼体标识等场景。采用优质材料，防水防尘，节能环保，使用寿命长。',
        content: '采用优质LED灯珠，亮度高、寿命长，支持多种颜色和字体设计。适用于店铺招牌、企业标识、广告牌等场景。专业团队设计制作，从方案设计到安装调试一站式服务。',
        priceMin: 280,
        priceMax: 680,
        priceRange: '280-680元/平方米',
        unit: '平方米',
        images: JSON.stringify([
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=LED%20illuminated%20signage%20letters%20storefront%20professional%20advertising%20night%20view&image_size=landscape_4_3',
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Acrylic%20LED%20channel%20letters%20night%20illumination%20commercial%20signage%20modern&image_size=landscape_4_3',
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Stainless%20steel%20backlit%20letters%20modern%20building%20facade%20premium&image_size=landscape_4_3',
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Resin%20LED%20letters%20colorful%20illuminated%20signage%20creative%20design&image_size=landscape_4_3'
        ]),
        features: JSON.stringify(['防水防尘IP65', '节能环保LED', '亮度可调节', '使用寿命10年+', '多种安装方式', '24小时售后服务']),
        craftType: 'led',
        craftTypes: JSON.stringify([
          {
            id: 'craft_001',
            name: '无边发光字',
            specifications: {
              material: '亚克力+LED灯带',
              thickness: '8-15mm',
              lightSource: 'LED灯带5050',
              installation: '墙面安装',
              warranty: '3年质保',
              waterproof: 'IP65防水等级',
              power: '8-12W/米',
              voltage: '12V DC',
              colorTemp: '3000K-6500K可选'
            },
            images: [
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Borderless%20LED%20channel%20letters%20acrylic%20modern%20storefront%20seamless&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Seamless%20illuminated%20letters%20night%20view%20commercial%20building%20elegant&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Acrylic%20LED%20letters%20uniform%20lighting%20professional%20signage&image_size=square'
            ],
            price: 280,
            unit: '平方米',
            isDefault: true,
            description: '无边框设计，光线均匀，视觉效果佳，适合现代商业场所',
            features: ['无边框设计', '光线均匀', '安装简便', '维护方便', '现代美观']
          },
          {
            id: 'craft_002',
            name: '迷你字',
            specifications: {
              material: '不锈钢+LED模组',
              thickness: '12-20mm',
              lightSource: 'LED模组2835',
              installation: '墙面/立柱安装',
              warranty: '5年质保',
              waterproof: 'IP67防水等级',
              power: '10-15W/米',
              voltage: '24V DC',
              colorTemp: '4000K-6000K'
            },
            images: [
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Mini%20LED%20letters%20stainless%20steel%20precision%20signage%20high%20end&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Small%20illuminated%20letters%20high%20end%20commercial%20signage%20luxury&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Precision%20LED%20mini%20letters%20premium%20building%20signage&image_size=square'
            ],
            price: 420,
            unit: '平方米',
            isDefault: false,
            description: '精工制作，高端大气，适合高档场所，超长使用寿命',
            features: ['精工制作', '高端材质', '超长寿命', '防腐蚀', '高档品质']
          },
          {
            id: 'craft_003',
            name: '树脂字',
            specifications: {
              material: '环氧树脂+LED',
              thickness: '15-25mm',
              lightSource: 'LED点光源',
              installation: '墙面安装',
              warranty: '2年质保',
              waterproof: 'IP54防水等级',
              power: '6-10W/米',
              voltage: '12V DC',
              colorTemp: '全彩可调'
            },
            images: [
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Resin%20LED%20letters%20colorful%20illuminated%20signage%20creative%20design&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Epoxy%20resin%20letters%20artistic%20LED%20lighting%20creative%20signage&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Colorful%20resin%20letters%20artistic%20illumination%20creative%20advertising&image_size=square'
            ],
            price: 680,
            unit: '平方米',
            isDefault: false,
            description: '可塑性强，色彩丰富，创意无限，适合艺术性场所',
            features: ['色彩丰富', '造型多样', '创意设计', '个性定制', '艺术效果']
          }
        ]),
        specifications: JSON.stringify({
          voltage: '12V/24V DC',
          power: '8-15W/米',
          colorTemp: '3000K-6500K可选',
          lifespan: '50000小时',
          workingTemp: '-20°C至+60°C',
          protection: 'IP65防护等级',
          controlSystem: '智能控制系统',
          dimming: '支持调光功能'
        }),
        status: 'published',
        sortOrder: 1
      },
      {
        id: createId(),
        title: '门头招牌',
        type: '标识导视',
        categoryId: getCategoryId('标识导视'),
        description: '专业门头招牌制作，多种材质工艺可选，为您的店铺打造独特的视觉形象。从设计到安装一站式服务，提供个性化定制方案。',
        content: '专业门头招牌设计制作，包括发光字门头、灯箱门头、不锈钢门头等多种样式。我们拥有专业的设计团队和制作工艺，为您的店铺打造独特的视觉形象，提升品牌价值。',
        priceMin: 180,
        priceMax: 800,
        priceRange: '180-800元/平方米',
        unit: '平方米',
        images: JSON.stringify([
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Modern%20storefront%20signage%20commercial%20shop%20entrance%20design%20professional&image_size=landscape_4_3',
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Restaurant%20entrance%20sign%20illuminated%20night%20view%20attractive%20modern&image_size=landscape_4_3',
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Aluminum%20composite%20panel%20storefront%20modern%20commercial%20signage%20clean&image_size=landscape_4_3',
          'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Stainless%20steel%20storefront%20sign%20premium%20commercial%20entrance%20luxury&image_size=landscape_4_3'
        ]),
        features: JSON.stringify(['个性设计', '多种材质', '专业安装', '售后保障', '快速制作', '性价比高']),
        craftType: 'traditional',
        craftTypes: JSON.stringify([
          {
            id: 'craft_006',
            name: '铝塑板门头',
            specifications: {
              material: '铝塑板+亚克力字',
              thickness: '4mm铝塑板',
              frame: '铝合金边框',
              installation: '钢结构安装',
              warranty: '2年质保',
              maintenance: '易清洁维护',
              fireRating: 'B1级防火',
              weatherResistance: '抗UV老化',
              colorOptions: '多种颜色可选'
            },
            images: [
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Aluminum%20composite%20panel%20storefront%20modern%20commercial%20signage%20clean&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=ACP%20shop%20sign%20professional%20installation%20commercial%20building&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Modern%20aluminum%20storefront%20sign%20business%20entrance%20professional&image_size=square'
            ],
            price: 180,
            unit: '平方米',
            isDefault: true,
            description: '经济实用，适合大部分商铺，安装快速，维护简单',
            features: ['经济实用', '安装快速', '维护简单', '性价比高', '防火阻燃']
          },
          {
            id: 'craft_007',
            name: '不锈钢门头',
            specifications: {
              material: '304不锈钢+LED',
              thickness: '1.5-2.0mm',
              surface: '拉丝/镜面处理',
              installation: '焊接安装',
              warranty: '5年质保',
              maintenance: '防腐耐用',
              corrosionResistance: '优异防腐性能',
              surfaceFinish: '多种表面处理',
              customization: '支持个性定制'
            },
            images: [
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Stainless%20steel%20storefront%20sign%20premium%20commercial%20entrance%20luxury&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Premium%20stainless%20steel%20shop%20sign%20high%20end%20commercial%20building&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Brushed%20stainless%20steel%20signage%20modern%20storefront%20elegant&image_size=square'
            ],
            price: 800,
            unit: '平方米',
            isDefault: false,
            description: '高端大气，适合高档场所，永不褪色，档次高',
            features: ['高端材质', '防腐耐用', '档次高', '永不褪色', '豪华质感']
          },
          {
            id: 'craft_008',
            name: '发光灯箱门头',
            specifications: {
              material: '铝合金边框+亚克力面板',
              thickness: '8-12mm亚克力',
              lightSource: 'LED灯条',
              installation: '悬挂/贴墙安装',
              warranty: '3年质保',
              maintenance: '定期清洁',
              brightness: '高亮度均匀发光',
              powerConsumption: '低功耗设计',
              weatherproof: 'IP65防护'
            },
            images: [
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=LED%20lightbox%20storefront%20sign%20illuminated%20commercial%20entrance&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Acrylic%20lightbox%20shop%20sign%20bright%20illumination%20modern&image_size=square',
              'https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=Backlit%20storefront%20lightbox%20professional%20commercial%20signage&image_size=square'
            ],
            price: 450,
            unit: '平方米',
            isDefault: false,
            description: '发光效果好，夜间识别度高，适合需要夜间展示的商铺',
            features: ['发光均匀', '夜间效果好', '节能环保', '安装灵活', '维护方便']
          }
        ]),
        specifications: JSON.stringify({
          designService: '免费设计',
          installationTime: '3-7个工作日',
          afterSales: '2年免费维护',
          customization: '支持个性定制',
          qualityStandard: '国家标准',
          safetyStandard: '安全认证',
          environmentalStandard: '环保材料'
        }),
        status: 'published',
        sortOrder: 2
      }
    ];
    
    // 插入产品数据
    for (const product of productData) {
      const existing = await db.select().from(products).where(eq(products.name, product.title)).limit(1);
      if (existing.length === 0) {
        await db.insert(products).values({
          ...product,
          name: product.title
        });
        console.log(`✅ 创建产品: ${product.title}`);
      } else {
        console.log(`ℹ️ 产品 ${product.title} 已存在，跳过创建`);
      }
    }
    
  } catch (error) {
    console.error('❌ 产品种子数据创建失败:', error);
    throw error;
  }
}